<?

require_once('DataObject/DomainModel.php');
require_once('Languages.php');

class LocaleDefaults extends DomainModel {
	protected $id;	//primaryKey=yes, dataType=int, maxLength=, allowNulls=no
	protected $languagesId;	//primaryKey=no, dataType=smallint, maxLength=, allowNulls=no
	protected $localeSectionsId;	//primaryKey=no, dataType=smallint, maxLength=, allowNulls=no
	protected $translation;	//primaryKey=no, dataType=text, maxLength=65535, allowNulls=no
	
	protected /*LocaleSection*/ $section;
	
	function getSection(){
		if (is_null($this->section)){
			$this->section = self::getFinder('LocaleSections')->find($this->localeSectionsId);
		}
		return $this->section;
	}
}

class LocaleDefaultsMapper extends ModelMapper {
	function __construct(){
		parent::__construct();
		$this->tableName = 'locale_defaults';
		$this->selectViewName = 'v_locale_defaults';
		$this->selectFields = '
			id,
			languages_id,
			locale_sections_id,
			translation,
			l_id,
			l_language,
			l_shortcut,
			ls_id,
			ls_section
		';

		$this->findQuery = self::$DB->prepare("
			SELECT ".$this->selectFields."
			FROM ".$this->selectViewName."
			WHERE id = ?
		");
		
		$this->insertQuery = self::$DB->prepare("
			CALL p_add_locale_defaults (@p_id,
				?,
				?,
				?
		)");


		$this->updateQuery = self::$DB->prepare("
			CALL p_upd_locale_defaults (
				?,
				?,
				?,
				?

		)");

		$this->deleteQuery = self::$DB->prepare("
			CALL p_del_locale_defaults (?)
		");

		$this->cacheTime = null;
	}

	protected function doLoad($array){
		$acuire = str_replace('Mapper', '', get_class($this));
		$obj = new $acuire($array['id']);
		$obj->setId($array['id']);
		$obj->setLanguagesId($array['languages_id']);
		$obj->setLocaleSectionsId($array['locale_sections_id']);
		$obj->setTranslation($array['translation']);
		
		$arr = ArrayUtils::splitByPrefix($array, array('l_', 'ls_'));
				
		if (!empty($arr['l_']))
			ModelMapper::instance('Languages')->loadArray($arr['l_']);
		if (!empty($arr['ls_']))
			ModelMapper::instance('LocaleSections')->loadArray($arr['ls_']);
			
		return $obj;
	}

	function insert(DomainModel $obj){
		
		$values = array(
			$obj->getLanguagesId(),
			$obj->getLocaleSectionsId(),
			$obj->getTranslation()
		);

		$this->doStatement($this->insertQuery, $values);
		$obj->setId(self::$DB->getOne("SELECT @p_id"));
	}

	function update(DomainModel $obj){
		
		$values = array(
			$obj->getId(),
			$obj->getLanguagesId(),
			$obj->getLocaleSectionsId(),
			$obj->getTranslation()
		);

		$this->doStatement($this->updateQuery, $values);
	}

	function delete(DomainModel $obj){
		$this->doStatement($this->deleteQuery, array($obj->getId()));
	}

	function newId(){

		$row = self::$DB->getRow('SHOW TABLE STATUS LIKE locale_defaults');
		return $row['Auto_increment'];

	}
}

class LocaleDefaultsCollection extends ModelCollection {
	function add(LocaleDefaults $model){
		$this->doAdd($model);
	}
}

class LocaleDefaultsCollectionExtended extends LocaleDefaultsCollection {
	private $stmt;
	private $valueArray;
	private $mapper;
	private $run = false;

	function __construct(ModelMapper $mapper, $stmtHandle, $valueArray){
		parent::__construct();
		$this->stmt = $stmtHandle;
		$this->valueArray = $valueArray;
		$this->mapper = $mapper;
	}

	function notifyAccess(){
		if (!$this->run){
			$result = $this->mapper->doStatement($this->stmt, $this->valueArray,$this->mapper->getCacheTime());
			$this->init_db($result, $this->mapper);
			$this->run = true;
		}
	}
}

?>